package com.ssy.lingxi.product.controller.pc;

import com.ssy.lingxi.common.constant.Constants;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.SelectUnitVO;
import com.ssy.lingxi.common.model.vo.SelectVO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.component.redis.service.IRedisStringUtils;
import com.ssy.lingxi.product.service.IBrandService;
import com.ssy.lingxi.product.service.customer.ICustomerCategoryService;
import com.ssy.lingxi.product.service.platform.ICategoryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @menu: 下拉框管理
 * 下拉框管理
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/30
 */
@RestController
@RequestMapping("/product/select")
public class SelectController extends BaseController {

    @Resource
    private ICustomerCategoryService customerCategoryService;

    @Resource
    private ICategoryService categoryService;

    @Resource
    private IBrandService brandService;

    @Resource
    private IRedisStringUtils redisStringUtils;

    /**
     * 查询品类下拉框--供应商
     * @param name 品类名称
     * @param memberId 供应商id
     * @param memberRoleId 供应商角色id
     * @return
     */
    @RequestMapping(value = "/getMemberCategory", method = RequestMethod.GET)
    public Wrapper<List<SelectVO>> getMemberCategory(@RequestParam(value = "name", required = false) String name, @RequestParam("memberId") Long memberId, @RequestParam("memberRoleId") Long memberRoleId) {
        return Wrapper.success(customerCategoryService.getSelectCustomerCategory(memberId, memberRoleId, name));
    }

    /**
     * 查询品牌下拉框--供应商
     * @param name 品牌名称
     * @param memberId 供应商id
     * @param memberRoleId 供应商角色id
     * @return
     */
    @RequestMapping(value = "/getMemberBrand", method = RequestMethod.GET)
    public Wrapper<List<SelectVO>> getMemberBrand(@RequestParam(value = "name", required = false) String name, @RequestParam("memberId") Long memberId, @RequestParam("memberRoleId") Long memberRoleId) {
        return Wrapper.success(brandService.getSelectBrand(memberId, memberRoleId, name));
    }

    /**
     * 查询品类下拉框--当前会员
     * @param name 品类名称
     * @return
     */
    @RequestMapping(value = "/getSelectCustomerCategory", method = RequestMethod.GET)
    public Wrapper<List<SelectVO>> getCustomerCategory(@RequestParam(value = "name", required = false) String name) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(customerCategoryService.getCustomerCategory(sysUser.getMemberId(), sysUser.getMemberRoleId(), name));
    }

    /**
     * 查询品类最大级别--当前会员
     * @return
     */
    @RequestMapping(value = "/getMaxCustomerCategory", method = RequestMethod.GET)
    public Wrapper<Integer> getMaxCustomerCategory() {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(customerCategoryService.getMaxCustomerCategory(sysUser));
    }

    /**
     * 根据父级id查询品类--当前会员
     * @return
     */
    @RequestMapping(value = "/getCustomerCategoryByParentId", method = RequestMethod.GET)
    public Wrapper<List<SelectVO>> getCustomerCategoryByParentId(@RequestParam("parentId") Long parentId) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(customerCategoryService.getCustomerCategoryByParentId(sysUser, parentId));
    }

    /**
     * 查询品牌下拉框--当前会员
     * @param name 品牌名称
     * @return
     */
    @RequestMapping(value = "/getSelectBrand", method = RequestMethod.GET)
    public Wrapper<List<SelectVO>> getSelectBrand(@RequestParam(value = "name", required = false) String name) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(brandService.getSelectBrand(sysUser.getMemberId(), sysUser.getMemberRoleId(), name));
    }

    /**
     * 查询中文单位下拉框--当前会员
     * @param name 单位名称
     * @return
     */
    @RequestMapping(value = "/getSelectCNUnit", method = RequestMethod.GET)
    public Wrapper<List<SelectUnitVO>> getSelectCNUnit(@RequestParam(value = "name", required = false) String name) {
        List<SelectUnitVO> selectVOList = new ArrayList<>();
        Map<String, String> unitMap = redisStringUtils.hGetAll(Constants.REDIS_KEY_UNIT, Constants.REDIS_PLATFORM_MANAGE_INDEX);
        for (Map.Entry<String, String> entry : unitMap.entrySet()) {
            String value = entry.getValue();
            String unitName = value.split(Constants.TRANSFER_STR + Constants.SPLIT_STR)[0];
            String unitEnglishShortName = value.split(Constants.TRANSFER_STR + Constants.SPLIT_STR)[1];
            if (StringUtils.isNotEmpty(name)) {
                if (unitName.contains(name) || unitEnglishShortName.contains(name)) {
                    SelectUnitVO selectUnitVO = new SelectUnitVO();
                    selectUnitVO.setId(Long.valueOf(entry.getKey()));
                    selectUnitVO.setName(unitName);
                    selectUnitVO.setEnglishShortName(unitEnglishShortName);
                    selectVOList.add(selectUnitVO);
                }
            } else {
                SelectUnitVO selectUnitVO = new SelectUnitVO();
                selectUnitVO.setId(Long.valueOf(entry.getKey()));
                selectUnitVO.setName(unitName);
                selectUnitVO.setEnglishShortName(unitEnglishShortName);
                selectVOList.add(selectUnitVO);
            }
            if (selectVOList.size() == Constants.SELECT_COUNT) {
                break;
            }
        }
        return Wrapper.success(selectVOList);
    }

    /**
     * 查询品类下拉框--平台后台
     * @param name 品类名称
     * @return
     */
    @RequestMapping(value = "/getSelectCategory", method = RequestMethod.GET)
    public Wrapper<List<SelectVO>> getSelectCategory(@RequestParam(value = "name", required = false) String name) {
        return Wrapper.success(categoryService.getSelectCategory(name));
    }

    /**
     * 查询品类最大级别--平台后台
     * @return
     */
    @RequestMapping(value = "/getMaxCategory", method = RequestMethod.GET)
    public Wrapper<Integer> getMaxCategory() {
        return Wrapper.success(categoryService.getCategoryMaxLevel());
    }

    /**
     * 根据父级id查询品类--平台后台
     * @return
     */
    @RequestMapping(value = "/getCategoryByParentId", method = RequestMethod.GET)
    public Wrapper<List<SelectVO>> getCategoryByParentId(@RequestParam("parentId") Long parentId) {
        return Wrapper.success(categoryService.getCategoryByParentId(parentId));
    }

    /**
     * 查询品牌下拉框--平台后台
     * @param name 品牌名称
     * @return
     */
    @RequestMapping(value = "/getSelectPlatformBrand", method = RequestMethod.GET)
    public Wrapper<List<SelectVO>> getSelectPlatformBrand(@RequestParam(value = "name", required = false) String name) {
        return Wrapper.success(brandService.getSelectPlatformBrand(name));
    }


}
